package component.auxiliares;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;

import component.auxiliares.pojo.TextoPojo;
import component.auxiliares.view.labelConfig;
import component.util.BaseMotionAdapter;

public class Texto extends JLabel {

	private JPopupMenu popupComponent = new JPopupMenu();
	private JMenuItem menuComponentRename = new JMenuItem("Renomear...");
	private JMenuItem menuComponentExclude = new JMenuItem("Excluir");
	private JMenuItem menuComponentProperties = new JMenuItem("Propriedades...");
	// private JMenuItem menuComponentResize = new JMenuItem("");
	// private JMenuItem menuComponentProperties = new
	// JMenuItem("Propriedades...");
	private JPanel panel;
	private TextoPojo pojo = new TextoPojo();
	private MouseListener[] mouseListeners;
	private MouseMotionListener[] mouseMotionListeners;

	public Texto(JPanel panel, String texto, MouseEvent evt) {
		this.setBounds(evt.getX(), evt.getY(), 60, 20);
		// this.setLocation(evt.getX(), evt.getY());
		panel.add(this);
		this.panel = panel;
		pojo.setX(evt.getX());
		pojo.setY(evt.getY());
		new BaseMotionAdapter(this, panel);
		this.setText(texto);
		pojo.setText(texto);

		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				lblMouseClicked(evt);
			}
		});

		menuComponentRename.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentRenameActionPerformed();
			}
		});
		menuComponentExclude.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentExcludeActionPerformed();
			}
		});
		menuComponentProperties.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				menuComponentPropertiesActionPerformed();
			}
		});

		popupComponent.add(menuComponentRename);
		popupComponent.add(menuComponentExclude);
		popupComponent.add(menuComponentProperties);

	}

	private void menuComponentRenameActionPerformed() {
		String text = JOptionPane.showInputDialog("Renomear...");
		this.setText(text);
		pojo.setText(text);
		this.repaint();
	}

	public void disableListeners() {
		mouseListeners = this.getMouseListeners();
		mouseMotionListeners = this.getMouseMotionListeners();
		for (MouseListener ml : mouseListeners) {
			this.removeMouseListener(ml);
		}
		for (MouseMotionListener mml : mouseMotionListeners) {
			this.removeMouseMotionListener(mml);
		}
	}

	public void enableListeners() {
		for (MouseListener ml : mouseListeners) {
			this.addMouseListener(ml);
		}
		for (MouseMotionListener mml : mouseMotionListeners) {
			this.addMouseMotionListener(mml);
		}
	}

	private void menuComponentExcludeActionPerformed() {
		String[] options = { "Ok", "Cancelar" };
		int result = JOptionPane.showOptionDialog(null, "Você tem certeza?",
				"Excluir", JOptionPane.OK_CANCEL_OPTION,
				JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
		if (result == 0) {
			try {
				panel.remove(this);
				panel.repaint();
				this.finalize();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
	}

	private void menuComponentPropertiesActionPerformed() {
		this.montaJanela();
	}

	public void setPojoConfig(String texto, Color color, JComboBox cmbTamanho,
			JComboBox cmbFonte) {
		// TODO
	}

	private void lblMouseClicked(MouseEvent evt) {
		if (evt.getClickCount() == 2) {
			this.montaJanela();
		} else if (evt.getButton() == evt.BUTTON3) {
			popupComponent.show(this, evt.getX(), evt.getY());
		}
	}

	public void atualiza(labelConfig frame) {
		Font font = frame.getFont();
		JTextField text = frame.getText();
		JComboBox cmbFontes = frame.getCmbFontes();
		JComboBox cmbTamanho = frame.getCmbTamanho();
		Color color = frame.getColor();
		font = new Font(cmbFontes.getSelectedItem().toString(), Font.PLAIN,
				Integer.parseInt(cmbTamanho.getSelectedItem().toString()));
		String texto = text.getText().trim();
		this.setForeground(color);
		this.setFont(font);
		this.setText(texto);
		pojo.setText(texto);
		pojo.setColor(String.valueOf(color.getRGB()));
		pojo.setFont(cmbFontes.getSelectedIndex());
		pojo.setSize(Integer.parseInt(cmbTamanho.getSelectedItem().toString()));
		frame.dispose();
	}

	private void montaJanela() {
		labelConfig frame = new labelConfig(this);
		Font font = frame.getFont();
		JTextField text = frame.getText();
		JComboBox cmbFontes = frame.getCmbFontes();
		JComboBox cmbTamanho = frame.getCmbTamanho();
		Color color = frame.getColor();
		JPanel btnCor = frame.getBtnCor();
		text.setText(pojo.getText());
		cmbFontes.setSelectedIndex(pojo.getFont());
		cmbTamanho.setSelectedItem(String.valueOf(pojo.getSize()));
		font = new Font(cmbFontes.getSelectedItem().toString(), Font.PLAIN,
				Integer.parseInt(cmbTamanho.getSelectedItem().toString()));
		try {
			color = new Color(Integer.parseInt(pojo.getColor()));
		} catch (Exception e) {
			pojo.setColor("00000000");
		}
		btnCor.setBackground(color);
		frame.setVisible(true);
	}

	@Override
	public void setLocation(int x, int y) {
		super.setLocation(x, y);
		pojo.setX(x);
		pojo.setY(y);
	}
}
